<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      class Observer {
        constructor() {
          //   this.message = {
          //     a: [fn1, fn2],
          //   };
          this.message = {};
        }
        $on(type, fn) {
          if (!this.message[type]) {
            this.message[type] = [];
          }
          this.message[type].push(fn);
        }
        $off(type, fn) {
          if (!this.message[type]) return;
          if (!fn) {
            this.message[type] = undefined;
            return;
          }
          this.message[type] = this.message[type].filter((item) => item !== fn);
        }
        $emit(type) {
          if (!this.message[type]) return;
          this.message[type].forEach((item) => {
            item();
          });
        }
      }
      let ob1 = new Observer();
      ob1.$on("onsend", () => {
        console.log("onsend");
      });
      ob1.$on("onsend", () => {
        console.log("onreceive");
      });

      console.log(ob1);
      ob1.$emit("onsend");
    </script>
  </body>
</html>
